﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace autoBidProject
{
    class MatchManager
    {
        private List<League> ibetLeagues, sboLeagues;
        private List<string[]> mapper;

        public void Update()
        {
            UpdateMapper();
        }

        private void UpdateMapper()
        {
            for (int i = 0; i < mapper.Count; i++)
            {
                if (GetMatch(ibetLeagues, mapper[i][0]) == null || GetMatch(sboLeagues, mapper[i][1]) == null)
                    mapper.RemoveAt(i);
            }
        }

        public Match GetRespectiveMatch(GlobalVar.MatchType srcMatchType, string srcMatchName)
        {
            List<League> targetMatchs = srcMatchType == GlobalVar.MatchType.IBET ? sboLeagues : ibetLeagues;
            int index = srcMatchType == GlobalVar.MatchType.IBET ? 0 : 1;

            foreach (string[] map in mapper)
                if (map[index].CompareTo(srcMatchName) == 0)
                    return GetMatch(targetMatchs,map[1-index]);

            return null;
        }

        private Match GetMatch( List<League> list, string name)
        {
            foreach (League league in list)
                foreach (Match match in league.Matchs)
                    if (match.Name().CompareTo(name) == 0)
                        return match;
            return null;
        }

        public void AutoMap()
        {
            if (ibetLeagues == null || sboLeagues == null)
                return;

            CleanMapper();


            List<Match> ibetMatchs = GetAllMatchs(ibetLeagues);
            List<Match> sboMatchs = GetAllMatchs(sboLeagues);

            for (int i = 0; i < ibetMatchs.Count; i++)
            {
                for (int j = 0; j < sboMatchs.Count; j++)
                    if (ibetMatchs[i].Same(sboMatchs[j]))
                    {
                        string[] a = new string[2];
                        a[0] = ibetMatchs[i].Name();
                        a[1] = sboMatchs[j].Name();
                        mapper.Add(a);
                    }
            }
        }

        private List<Match> GetAllMatchs(List<League> leagues)
        {
            List<Match> matchs = new List<Match>();
            foreach(League league in leagues)
                foreach(Match match in league.Matchs)
                    matchs.Add(match);
            return matchs;
        }

        public void CleanMapper()
        {
            if (mapper == null)
                mapper = new List<string[]>();
            else
                mapper.RemoveRange(0, mapper.Count);
        }

        public void MapEvent(string ibetName, string sboName)
        {
            mapper.Add(new string[] {ibetName, sboName});
        }

        public List<Match> GetMappedMatchs(GlobalVar.MatchType matchType)
        {
            int index = matchType == GlobalVar.MatchType.IBET == true ? 0 : 1;
            List<League> leagues = matchType == GlobalVar.MatchType.IBET == true ? ibetLeagues : sboLeagues;
            List<Match> matchs = new List<Match>();
            if (leagues != null)
            {
                foreach (League league in leagues)
                {
                    bool bMapped = false;
                    foreach (Match match in league.Matchs)
                    {
                        string name = match.Name();
                        foreach (string[] nameMapped in mapper)
                            if (name.CompareTo(nameMapped[index]) == 0)
                            {
                                bMapped = true;
                                break;
                            }

                        if (bMapped)
                            matchs.Add(match);
                    }
                }
            }
            return matchs;
        }

        public List<Match> GetNoneMappedMatchs(GlobalVar.MatchType matchType)
        {
            int index = matchType == GlobalVar.MatchType.IBET ? 0 : 1;
            List<Match> matchs = new List<Match>();
            List<League> leagues = matchType == GlobalVar.MatchType.IBET == true ? ibetLeagues : sboLeagues;

            if (leagues != null)
            {
                foreach (League league in leagues)
                {
                    bool bNoneMap = true;
                    foreach (Match match in league.Matchs)
                    {
                        string name = match.Name();
                        foreach (string[] nameMapped in mapper)
                            if (name.CompareTo(nameMapped[index]) == 0)
                            {
                                bNoneMap = false;
                                break;
                            }

                        if (bNoneMap)
                            matchs.Add(match);
                    }
                }
            }
            return matchs;
        }

        //private Match GetRespectiveEvent(string sourceMatchName, GlobalVar.HDP eventHDP)
        //{
        //    foreach (string[] map in mapper)
        //        if (map[0].CompareTo(ibetName) == 0)
                    
        //            return GetMatch(sboLeagues, map[1]);
        //    return null;
        //}

        public MatchManager()
        {
            ibetLeagues = new List<League>();
            sboLeagues = new List<League>();
            mapper = new List<string[]>();
        }

        #region Get & Set properties

        internal List<League> SboLeagues
        {
            get { return sboLeagues; }
            set { sboLeagues = value; }
        }

        internal List<League> IbetLeagues
        {
            get { return ibetLeagues; }
            set { ibetLeagues = value; }
        }
        public List<string[]> Mapper
        {
            get { return mapper; }
        }

        #endregion 
    
        public bool Map(string ibetMatch, string sboMatch)
        {
            for (int i = 0; i < mapper.Count; i++)
            {
                if (ibetMatch.CompareTo(mapper[i][0]) == 0)
                    return false;
            }
            mapper.Add(new string[] { ibetMatch, sboMatch });
            return true;
        }

        public void RemoveMap(string match)
        {
            for (int i = 0; i < mapper.Count; i++)
            {
                if (match.CompareTo(mapper[i][0]) == 0)
                {
                    mapper.RemoveAt(i);
                    break;
                }
            }
        }
    }
}
